package com.sghd.candy.module.common.event;

import com.sghd.candy.module.player.manager.Player;
import com.sghd.common.event.Event;
import com.sghd.common.socket.session.Session;
import com.sghd.common.socket.utils.IpUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * 登出事件体
 */
public class LogoutEvent implements IdentityEvent {

    /** 事件名 */
    public static final String NAME = "common:logout";

    /** 登出的用户 */
    private Player player;
    /** 登出事件触发原因 */
    private int cause;
    /** 会话 */
    private Session session;
    /** 会话对应的IP地址 */
    private String ip;

    // Getter and Setter ...

    @Override
    public long getOwner() {
        return player.getId();
    }

    public int getCause() {
        return cause;
    }

    @Override
    public String getName() {
        return NAME;
    }

    public Session getSession() {
        return session;
    }

    public String getIp() {
        return ip;
    }

    public Player getPlayer() {
        return player;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    // Static Method's ...

    /** 构造方法 */
    public static Event<LogoutEvent> valueOf(Player player, int cause, Session session) {
        LogoutEvent body = new LogoutEvent();
        body.player = player;
        body.session = session;
        body.cause = cause;
        body.ip = IpUtils.getIp(session);
        return Event.valueOf(NAME, body);
    }

}
